package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISocProgItemDAO;
import org.jsola.hr.dto.SocProgItemAddDTO;
import org.jsola.hr.dto.SocProgItemUpdateDTO;
import org.jsola.hr.entity.SocProgItemDO;
import org.jsola.hr.query.SocProgItemQuery;
import org.jsola.hr.service.ISocProgItemService;
import org.jsola.hr.vo.SocProgItemListVO;
import org.jsola.hr.vo.SocProgItemVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 参保方案表子项
 *
 * @author zhr
 */
@Service("hrSocProgItemServiceImpl")
public class SocProgItemServiceImpl implements ISocProgItemService {

    @Autowired
    private ISocProgItemDAO socProgItemDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SocProgItemVO save(SocProgItemAddDTO socProgItemAddDTO, TokenUser tokenUser) {
        // 转DO
        SocProgItemDO socProgItemDO = socProgItemAddDTO.to(SocProgItemDO.class);
        // 保存
        socProgItemDO = save(socProgItemDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return socProgItemDO.to(SocProgItemVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public List<SocProgItemVO> batchSave(List<SocProgItemAddDTO> socProgItemAddDTOList, TokenUser tokenUser) {
        if (CollectionUtils.isEmpty(socProgItemAddDTOList)) {
            return null;
        }
        List<SocProgItemDO> socProgItemDOList = socProgItemAddDTOList
                .parallelStream()
                .map(socProgItemAddDTO -> {
                    SocProgItemDO socProgItemDO = socProgItemAddDTO.to(SocProgItemDO.class);
                    socProgItemDO.preInsert(tokenUser.getUserId());
                    socProgItemDO.setSiteId(tokenUser.getSiteId());
                    return socProgItemDO;
                }).collect(Collectors.toList());
        socProgItemDAO.insertListAndSetId(socProgItemDOList);
        return socProgItemDOList
                .parallelStream()
                .map(socProgItemDO -> socProgItemDO.to(SocProgItemVO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SocProgItemUpdateDTO socProgItemUpdateDTO, TokenUser tokenUser) {
        //转DO
        SocProgItemDO socProgItemDO = socProgItemUpdateDTO.to(SocProgItemDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(socProgItemDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... socProgItemIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) socProgItemIds);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteBySocProgId(String socProgId, TokenUser tokenUser) {
        SocProgItemDO socProgItemDO = new SocProgItemDO();
        socProgItemDO.setSiteId(tokenUser.getSiteId());
        socProgItemDO.setProgId(socProgId);
        return socProgItemDAO.delete(socProgItemDO);
    }


    @Override
    public SocProgItemVO selectById(String socProgItemId, String siteId) {
        SocProgItemDO socProgItemDO = selectDOById(socProgItemId, siteId);
        if (socProgItemDO == null) {
            return null;
        }
        return socProgItemDO.to(SocProgItemVO.class);
    }

    @Override
    public List<SocProgItemListVO> select(SocProgItemQuery socProgItemQuery, String siteId) {
        List<SocProgItemDO> socProgItemDOList = selectDO(socProgItemQuery, siteId);
        if (CollectionUtils.isEmpty(socProgItemDOList)) {
            return socProgItemDOList == null ? null : new ArrayList<>();
        }
        return socProgItemDOList.stream()
                .map(socProgItemDO -> socProgItemDO.to(SocProgItemListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SocProgItemQuery socProgItemQuery, String siteId) {
        Example example = buildExample(socProgItemQuery, siteId);
        return socProgItemDAO.selectCountByExample(example);
    }

    @Override
    public Page<SocProgItemListVO> selectPage(SocProgItemQuery socProgItemQuery, String siteId) {
        Example example = buildExample(socProgItemQuery, siteId);
        Page<SocProgItemDO> page = socProgItemDAO.selectPageByExample(example,
                socProgItemQuery.getPageNo(),
                socProgItemQuery.getPageSize());

        return page.to(SocProgItemListVO.class);
    }

    @Override
    public SocProgItemDO selectDOById(String socProgItemId, String siteId) {
        return listById(socProgItemId, siteId);
    }

    @Override
    public List<SocProgItemDO> selectDO(SocProgItemQuery socProgItemQuery, String siteId) {
        Example example = buildExample(socProgItemQuery, siteId);
        return socProgItemDAO.selectByExample(example);
    }

    @Override
    public List<SocProgItemDO> selectBySocProgId(String socProgId, String siteId) {
        Example example = new Example(SocProgItemDO.class);
        example.and()
                .andEqualTo("progId", socProgId)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        example.orderBy("sort");
        return socProgItemDAO.selectByExample(example);
    }

    @Override
    public List<SocProgItemDO> selectBySocProgIds(List<String> schemeIdList, TokenUser tokenUser) {
        if (CollectionUtils.isEmpty(schemeIdList)) {
            return new ArrayList<>();
        }
        return socProgItemDAO.selectBySocSchemeIds(schemeIdList, tokenUser.getSiteId());
    }

    @Override
    public SocProgItemDO selectBySocProgIdAndItemName(String socProgId, String itemName, String siteId) {
        Example example = new Example(SocProgItemDO.class);
        example.and()
                .andEqualTo("progId", socProgId)
                .andEqualTo("itemName", itemName)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        return socProgItemDAO.selectOneByExample(example);
    }

    @Override
    public List<SocProgItemDO> selectBySocProgIdAndItemType(String socProgId, Integer itemType, TokenUser tokenUser) {
        Example example = new Example(SocProgItemDO.class);
        example.and()
                .andEqualTo("progId", socProgId)
                .andEqualTo("itemType", itemType)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId());
        return socProgItemDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param socProgItemQuery 查询参数
     * @param siteId           所属站点id
     * @return example
     */
    private Example buildExample(SocProgItemQuery socProgItemQuery, String siteId) {
        Example example = new Example(SocProgItemDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (socProgItemQuery.getProgId() != null) {
            example.and().andEqualTo("progId", socProgItemQuery.getProgId());
        }
        if (socProgItemQuery.getItemType() != null) {
            example.and().andEqualTo("itemType", socProgItemQuery.getItemType());
        }
        if (socProgItemQuery.getItemName() != null) {
            example.and().andEqualTo("itemName", socProgItemQuery.getItemName());
        }
        if (socProgItemQuery.getCardinalityDefaultStart() != null &&
                socProgItemQuery.getCardinalityDefaultEnd() != null) {
            example.and().andBetween("cardinalityDefault", socProgItemQuery.getCardinalityDefaultStart(),
                    socProgItemQuery.getCardinalityDefaultEnd());
        }
        if (socProgItemQuery.getCardinalityDefaultAmount() != null) {
            example.and().andEqualTo("cardinalityDefaultAmount", socProgItemQuery.getCardinalityDefaultAmount());
        }
        if (socProgItemQuery.getCardinalityMinStart() != null &&
                socProgItemQuery.getCardinalityMinEnd() != null) {
            example.and().andBetween("cardinalityMin", socProgItemQuery.getCardinalityMinStart(),
                    socProgItemQuery.getCardinalityMinEnd());
        }
        if (socProgItemQuery.getCardinalityMaxStart() != null &&
                socProgItemQuery.getCardinalityMaxEnd() != null) {
            example.and().andBetween("cardinalityMax", socProgItemQuery.getCardinalityMaxStart(),
                    socProgItemQuery.getCardinalityMaxEnd());
        }
        if (socProgItemQuery.getPersentCompanyStart() != null &&
                socProgItemQuery.getPersentCompanyEnd() != null) {
            example.and().andBetween("persentCompany", socProgItemQuery.getPersentCompanyStart(),
                    socProgItemQuery.getPersentCompanyEnd());
        }
        if (socProgItemQuery.getPersentPersonalStart() != null &&
                socProgItemQuery.getPersentPersonalEnd() != null) {
            example.and().andBetween("persentPersonal", socProgItemQuery.getPersentPersonalStart(),
                    socProgItemQuery.getPersentPersonalEnd());
        }
        if (socProgItemQuery.getAmountCompanyStart() != null &&
                socProgItemQuery.getAmountCompanyEnd() != null) {
            example.and().andBetween("amountCompany", socProgItemQuery.getAmountCompanyStart(),
                    socProgItemQuery.getAmountCompanyEnd());
        }
        if (socProgItemQuery.getAmountPersonalStart() != null &&
                socProgItemQuery.getAmountPersonalEnd() != null) {
            example.and().andBetween("amountPersonal", socProgItemQuery.getAmountPersonalStart(),
                    socProgItemQuery.getAmountPersonalEnd());
        }
        if (socProgItemQuery.getMantissaProcCompany() != null) {
            example.and().andEqualTo("mantissaProcCompany", socProgItemQuery.getMantissaProcCompany());
        }
        if (socProgItemQuery.getMantissaProcPersonal() != null) {
            example.and().andEqualTo("mantissaProcPersonal", socProgItemQuery.getMantissaProcPersonal());
        }
        if (socProgItemQuery.getSort() != null) {
            example.and().andEqualTo("sort", socProgItemQuery.getSort());
        }
        if (socProgItemQuery.getDefaultType() != null) {
            example.and().andEqualTo("defaultType", socProgItemQuery.getDefaultType());
        }
        if (socProgItemQuery.getItemRemark() != null) {
            example.and().andEqualTo("itemRemark", socProgItemQuery.getItemRemark());
        }
        // 排序
        ExampleKit.setExampleOrder(example, socProgItemQuery.getOrders());
        return example;
    }
}




